﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.HtmlControls;

namespace CRPTModularCorrousel
{
    public class ModularRepeaterTemplate: ITemplate
    {
        public ModularRepeaterTemplate(ModularRepeaterTemplate repeat) {
            this.ImageExtention = repeat.ImageExtention;
            this.RepeaterPath = repeat.RepeaterPath;
            this.RepeaterTag = !String.IsNullOrEmpty(repeat.RepeaterTag) ? repeat.RepeaterTag : "";
        }

        public ModularRepeaterTemplate()
        {
        }

        private const string STYLE_BACKGROUND = "background-image: url({0});background-size: 965px 400px;";
        private const string IMAGE_COMBINE = "{0}/{1}{2}.{3}";

        [Browsable(true)]
        [Category("Behavior")]
        public string ImageExtention { set; get; }

        public int CurrentIndex { set; get; }

        [Browsable(true)]
        [Category("Behavior")]
        public string RepeaterPath { set; get; }

        [Browsable(true)]
        [Category("Behavior")]
        public int RepeaterCount { set; get; }

        [Browsable(true)]
        [Category("Behavior")]
        public string RepeaterTag { set; get; }

        public void InstantiateIn(Control container)
        {
            var li = new HtmlGenericControl("li");
            var paragraph = new HtmlGenericControl("p");
            if (!String.IsNullOrEmpty(RepeaterPath))
            {
                string imagePath = String.Format(IMAGE_COMBINE, RepeaterPath, RepeaterTag, CurrentIndex, ImageExtention);
                imagePath = String.Format(STYLE_BACKGROUND, imagePath);
                paragraph.Attributes.Add("style", imagePath);
            }
            li.Controls.Add(paragraph);
            container.Controls.Add(li);
        }
    }
}
